package oceanlife;
import java.awt.Color;
import java.awt.Component;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.lang.Thread.UncaughtExceptionHandler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingWorker;

import oceanlife.evostrat.Gene;
import oceanlife.log.FCLogger;
import oceanlife.log.Logger;
import oceanlife.pathfinding.Line2;
import oceanlife.pathfinding.Obstacles;
import oceanlife.pathfinding.Pillar;
import oceanlife.pathfinding.Pnt;
import oceanlife.pathfinding.SimpleGraphBuilder;


public class FitnessCheckWorker extends GenStarterWorker{
		private SimpleSimPanel simPanel;
		private Gene gene;

		public FitnessCheckWorker(ArrayList<SimpleSimPanel> simPanels,ArrayList<AgentCustomSettings> agentSpawns, JLabel info,Gene gene) {
			super(simPanels, agentSpawns, info);
			simPanel = simPanels.get(0);
			this.gene = gene;
		}
		
		protected Integer doInBackground() throws Exception
        {
			try {	
				ArrayList<ArrayList<ArrayList<Agent>>> agentScenarios = new ArrayList<ArrayList<ArrayList<Agent>>>();
				ArrayList<Pnt> agentScenarioGoals = new ArrayList<Pnt>();
				generateAgents(agentSpawns,agentScenarios,agentScenarioGoals);

				Date dateNow = new Date ();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd...HH_mm_ss.SSS");
				StringBuilder dateString = new StringBuilder( sdf.format( dateNow ) );
				FCLogger.write("Started_at: "+dateString.toString());
				FCLogger.write("Seed: "+RandomGen.getSeed());
				FCLogger.write("Testset: "+OLSet.get("testSet"));
				FCLogger.write("Agent_spawn_interval: "+OLSet.getInt("spawnInterval"));
				FCLogger.write("Scenario_count: "+OLSet.getInt("scenarioCount"));
				FCLogger.write("Agents_per_Scenario: "+OLSet.getInt("agentsPerScenario"));
				int IDCounter = 1;
    			simPanel.addAgentScenarios(agentScenarios,agentScenarioGoals);
    			String agentsUsed = "";
    		
    			for(ArrayList<ArrayList<Agent>> scenario : simPanel.getAgentScenarios()){
        			for(ArrayList<Agent> side : scenario){
            			agentsUsed += "/"+side.size();
        			}
        			agentsUsed += " ";
    			}
    			RandomGen rand = RandomGen.getNextRandomGen();
    			dateNow = new Date();
    			dateString = new StringBuilder( sdf.format( dateNow ) );
    			String ID = OLSet.get("testSet")+"-"+OLSet.getInt("spawnInterval")+"-"+dateString.toString()+"-"+IDCounter;
    			simPanel.readGenes(gene,rand,ID,false);
	            GenWorker gw = new GenWorker(simPanel);
	            gw.execute();
	            IDCounter++;
				while(!gw.isDone()){
					Thread.sleep(100);
				}
				FCLogger.write(gene.printGenes());
				FCLogger.write("Fitnesses: "+simPanel.getFitnessesString());
	    		FCLogger.write("Fitness: "+simPanel.getTotalAgentCycles());
	    		FCLogger.write("Fitness/Agent: "+(simPanel.getTotalAgentCycles()*1.0/OLSet.getInt("agentsPerScenario")/OLSet.getInt("scenarioCount")));
	    		info.setText(Integer.toString(simPanel.getTotalAgentCycles()));
	    		
				dateNow = new Date ();
				sdf = new SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss z");
				dateString = new StringBuilder( sdf.format( dateNow ) );
				FCLogger.write("Stopped_at:"+dateString.toString());
	    		FCLogger.write("");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return 0;
        }

        protected void done()
        {
                try
                {
                
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
        }


}